package com.ricchieri.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.dao.interf.IEmailToSendDao;
import com.ricchieri.filter.EmailToSendFilter;
import com.ricchieri.model.EmailToSend;
import com.ricchieri.util.DateUtil;
import com.ricchieri.util.StringUtils;

public class EmailToSendDao extends GenericDao implements IEmailToSendDao {
	private static final long serialVersionUID = 6277197784526325616L;

	@Override
	@SuppressWarnings("unchecked")
	public List<EmailToSend> getAllEmailToSend(EmailToSendFilter emailToSendFilter) {
		Criteria criteria = criteriaAllEmailToSend(emailToSendFilter);
		
		return criteria.list();
	}

	@Override
	public int getAllEmailToSendPaginatedCountByFilter(EmailToSendFilter emailToSendFilter) {
		Criteria criteria = criteriaAllEmailToSend(emailToSendFilter);
		Long result = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
		return new Integer(result.intValue());		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EmailToSend> getAllEmailToSendPaginatedByFilter(EmailToSendFilter emailToSendFilter, int startingAt) {
		Criteria criteria = criteriaAllEmailToSend(emailToSendFilter);
		criteria.setMaxResults(BusinessSettings.ROW_MAX_RESULT);
		criteria.setFirstResult(startingAt);
		
		return criteria.list();
	}

	private Criteria criteriaAllEmailToSend(EmailToSendFilter emailToSendFilter) {
		Criteria criteria = super.criteriaAllBasic(emailToSendFilter, EmailToSend.class);

		boolean joinUser = StringUtils.isNotEmpty(emailToSendFilter.getDni()) 
				|| StringUtils.isNotEmpty(emailToSendFilter.getFirstName())
				|| StringUtils.isNotEmpty(emailToSendFilter.getLastName());
		
		if(joinUser) {
			
			criteria.createAlias(getTableName().concat(".user"), "user");
			if(StringUtils.isNotEmpty(emailToSendFilter.getDni())) {
				criteria.add(Restrictions.eq("user.dni", emailToSendFilter.getDni()));
			}
			
			if(StringUtils.isNotEmpty(emailToSendFilter.getFirstName())) {
				criteria.add(Restrictions.like("user.firstName", "%".concat(emailToSendFilter.getFirstName()).concat("%")));		
			}
			
			if(StringUtils.isNotEmpty(emailToSendFilter.getLastName())) {
				criteria.add(Restrictions.like("user.lastName", "%".concat(emailToSendFilter.getLastName()).concat("%")));	
			}
		}
		
		if(StringUtils.isNotEmpty(emailToSendFilter.getDocumentType())) {
			
			if(!joinUser) {
				criteria.createAlias(getTableName().concat(".user"), "user");
			}

			criteria.createAlias("user.documentType", "documentType");
			criteria.add(Restrictions.eq("documentType.id", emailToSendFilter.getDocumentTypeId()));
		}
		
		if (emailToSendFilter.getStart() != null) {
			criteria.add(Restrictions.ge(getTableName().concat(".sendDate"), DateUtil.changeToStartDay(emailToSendFilter.getStart())));
		}
		
		if(emailToSendFilter.getFinish() != null) {
			criteria.add(Restrictions.le(getTableName().concat(".sendDate"), DateUtil.changeToEndDay(emailToSendFilter.getFinish())));
		}
		
		if(StringUtils.isNotEmpty(emailToSendFilter.getEmailToSendType())){
			criteria.createAlias(getTableName().concat(".emailToSendType"), "emailToSendType");
			criteria.add(Restrictions.eq("emailToSendType.id", emailToSendFilter.getEmailToSendTypeId()));
		}
		
		if(emailToSendFilter.getFindIsMessageSent()) {
			criteria.add(Restrictions.eq(getTableName().concat(".isMessageSent"), emailToSendFilter.getIsMessageSent()));
		}
		
		if(emailToSendFilter.getAttempCount() > 0) {
			criteria.add(Restrictions.le(getTableName().concat(".attemptCounter"), emailToSendFilter.getAttempCount()));
		}
		
		return criteria;
	}

	@Override
	protected String getTableName() {
		return "emailToSend";
	}
}
